<?php

namespace App\Admin\Controllers;

use App\Category;
use App\News;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Auth\Permission;
use Encore\Admin\Layout\Content;
use App\Admin\Actions\News\Restore;
use App\Admin\Actions\News\BatchRestore;

class NewsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '新闻';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new News());

        $grid->model()->with('category')->latest();
        $grid->filter(function ($filter) {

            // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
            $filter->scope('trashed', '回收站')->onlyTrashed();

        });

        if (request('category')) {
            $grid->model()->where('category_id', request('category'));
        }
        $grid->column('id', __('Id'));
        $grid->column('title', __('Title'))->editable()->style('max-width:200px;word-break:break-all;');
        $grid->column('thumb', '缩略图')->image();
        $grid->column('category', '分类')->display(function ($category) {
            return '<a href="' . admin_url('news') . '?category=' . $category['id'] . '">' . $category['name'] . '</a>';
        });
        // $grid->column('summary', __('Summary'))->style('max-width:200px;word-break:break-all;');
        $grid->column('view_counts', __('View counts'))->style('text-align:center');
        $grid->column('comment_counts', __('Comment counts'));
        $grid->column('thumb_up_counts', __('Thumb up counts'));
        // $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));
        $grid->quickSearch('title', 'summary', 'body');
        $grid->actions(function ($actions) {
            // 没有`delete-news`权限的角色不显示删除按钮
            if (!Admin::user()->can('delete-news')) {
                $actions->disableDelete();
            }

            if (\request('_scope_') == 'trashed') {
                $actions->disableDelete();
                $actions->add(new Restore());
            }
        });
        $grid->batchActions(function ($batch) {

            if (\request('_scope_') == 'trashed') {
                $batch->disableDelete();
                $batch->add(new BatchRestore());
            }

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $model = News::findOrFail($id);
        $show = new Show($model);

        $show->field('id', __('Id'));
        $show->field('title', __('Title'));
        $show->field('summary', __('Summary'));
        $show->field('thumb', __('缩略图'))->image();
        $show->field('view_counts', __('浏览量'));
        $show->field('comment_counts', __('评论量'));
        $show->field('thumb_up_counts', __('点赞量'));
        $show->divider();
        // $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->divider();
        $show->field('body', __('Body'))->body($model->body, $model, false, false);


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new News());

        $form->text('title', __('Title'));
        $form->select('category_id', '分类')->options(Category::selectOptions());
        $form->textarea('summary', __('Summary'));
        $form->image('thumb', __('缩略图'));
        $form->ckeditor('body', __('Body'));
        // $form->number('view_counts', __('View counts'));
        // $form->number('comment_counts', __('Comment counts'));
        // $form->number('thumb_up_counts', __('Thumb up counts'));

        return $form;
    }

    public function create(Content $content)
    {
        Permission::check('create-post');
        return $content
            ->title($this->title())
            ->description($this->description['create'] ?? trans('admin.create'))
            ->body($this->form());
    }
}
